<?php
    class AdminMenu extends iJBlock
    {
        /**
         *
         * This function is construct Languages
         *
         *
         * @access public
         */
        public function __construct() {
        }
        
        /**
         * execute
         *
         * Executes the block to generate the tokens
         *
         * @return array Array with the tokens of the block
         *
         * @access public
         */
        public function execute() {
          $iJDataBase =  iJFactory::singleton('iJDataBase');
          $QMenu = $iJDataBase->query('SELECT
                                        M.id,
                                        M.parrent_id,
                                        def.value as title,
                                        M.sortOrder,
                                        M.url,
                                        M.linkType
                                      FROM :table_admin_menu AS M
                                           :lang
                                      ORDER BY M.sortOrder DESC');
                                      
          $QMenu->bindTable(':table_admin_menu', TABLE_ADMIN_MENU);
          $QMenu->bindLaguageDefinitions(':lang', TABLE_ADMIN_MENU, 'M');
          $QMenu->execute();
          $Menu = array();
          
          while($QMenu->next()) {
            $Menu[$QMenu->value('id')]['parrent_id']     = $QMenu->value('parrent_id');
            $Menu[$QMenu->value('id')]['title']          = $QMenu->value('title');
            $Menu[$QMenu->value('id')]['sortOrder']      = $QMenu->value('sortOrder');
            
            if($QMenu->value('linkType') == 'inner') {
                $Menu[$QMenu->value('id')]['url']        = iJHrefLink::linkToLocalPage($QMenu->value('url'));
            } else {
                $Menu[$QMenu->value('id')]['url']        = $QMenu->value('url');    
            }
          }

          $NewMenu  = array();
          foreach($Menu as $id => $item) {
            if($item['parrent_id'] != 0) {
                if(isset($NewMenu[$item['parrent_id']])) {
                   $NewMenu[$item['parrent_id']]['submenu'][$id] = $item; 
                   unset($Menu[$id]);
                } else {
                   
                }
            } else {
              $NewMenu[$id] = $item;
              unset($Menu[$id]);
            } 
          }
          
          foreach($Menu as $id => $item) {
             foreach ($NewMenu as $itemID => $mItem) {
                  if(isset($mItem['submenu'])) {
                     if(isset($mItem['submenu'][$item['parrent_id']])) {
                          //$mItem['submenu'][$item['parrent_id']]['submenu'][$id] = $item; 
                          $NewMenu[$itemID]['submenu'][$item['parrent_id']]['submenu'][$id] = $item;
                     } 
                  }
                  
              }
          }
          
          $this->addToken('Menu', $NewMenu);
          $this->addToken('Home', iJHrefLink::linkToLocalPage('admin_home'));
          $this->addToken('LogOut', iJHrefLink::linkToLocalPage('admin_login') . '?logout');
          return parent::execute();
        }
        
        
        public function getMenuList($page, $search, $parent, $limit = '') {
          $iJDataBase =  iJFactory::singleton('iJDataBase');
          $QMenu = $iJDataBase->query('SELECT
                                        M.id,
                                        M.parrent_id,
                                        def.value as title,
                                        M.sortOrder,
                                        M.url,
                                        M.linkType
                                      FROM :table_admin_menu AS M
                                           :lang
                                     ');
                                      
          $QMenu->bindTable(':table_admin_menu', TABLE_ADMIN_MENU);
          $QMenu->bindLaguageDefinitions(':lang', TABLE_ADMIN_MENU, 'M');
         // spr($QBlocks); die();
          
            
          if(!empty($search)) {
            $QMenu->appendQuery('AND title like :search');
            $QMenu->bindValue(':search', '%' . $search . '%');
          } else {
            $QMenu->appendQuery('AND parrent_id = :parent');
            $QMenu->bindValue(':parent',  $parent);
          }
          
          $QMenu->appendQuery('ORDER BY M.sortOrder DESC');
           
          $QMenu->appendQuery(':limit');
          $QMenu->bindTable(':limit', $QMenu->addLimit($page, $limit));
          
          $QMenu->execute();
        //  spr($QMenu);
          $menu = array();
          while($QMenu->next()) {
            $menu[$QMenu->value('id')]['id']             = $QMenu->value('id');
            $menu[$QMenu->value('id')]['title']          = $QMenu->value('title');
            $menu[$QMenu->value('id')]['url']            = $QMenu->value('url');
            $menu[$QMenu->value('id')]['linkType']       = $QMenu->value('linkType');
            $menu[$QMenu->value('id')]['sortOrder']      = $QMenu->value('sortOrder');
           // $menu[$QMenu->value('id')]['edit']           = iJHrefLink::linkToLocalPage('admin_menu',   iJHrefLink::Icon('edit',  '16x16'), array('key'   => $QMenu->value('id')));
            $menu[$QMenu->value('id')]['trash']          = iJHrefLink::linkToLocalPage('admin_menu') . '?delID='  . $QMenu->value('id');
            $menu[$QMenu->value('id')]['link']           = iJHrefLink::linkToLocalPage('admin_menu') . '?parent=' . $QMenu->value('id');
            /*
            /*
            if($QMenu->value('linkType') == 'inner') {
                $menu[$QMenu->value('id')]['url']        = iJHrefLink::linkToLocalPage($QMenu->value('url'));
            } else {
               $menu[$QMenu->value('id')]['url']        = $QMenu->value('url');    
            }*/
          }
             // spr($menu);
          return array($menu, $QMenu);
          
        }
        
        
        public function getBackLink($parent) {
          $iJDataBase =  iJFactory::singleton('iJDataBase');
          $QMenu = $iJDataBase->query('SELECT
                                        parrent_id
                                      FROM :table_admin_menu
                                      WHERE id = :id
                                     ');
                                      
          $QMenu->bindTable(':table_admin_menu', TABLE_ADMIN_MENU);
          $QMenu->bindValue(':id', $parent);
          $QMenu->execute();
          //spr($QMenu);
          if($QMenu->next()) {
            return iJHrefLink::linkToLocalPage('admin_menu') . '?parent=' . $QMenu->value('parrent_id');
          } else {
            return '#';
          }
          
          
        }
        
    }
?>